import SystemApi from '@/api/SystemApi'
import config from '@/config'
import i18n from '@/i18n'
import RegionApi from '@/api/basebiz/RegionApi'
import userInfoApi from '@/api/basebiz/UserInfoApi'
import SysNoticeApi from '@/api/basebiz/SysNoticeApi'
import router from '@/router'

/**
 * 登录信息
 */
export default {
  state: {
    isInitialPwd: false,
    info: {},
    themeList: [
      // 主题列表
      'dark-theme',
      'red-theme',
      'green-theme',
      'blue-theme'
    ],
    currentTheme: null, // 当前主题
    columnsConfigData: [],
    avatarData: null,
    sysNoticeCount: 0 // 系统通知未读个数
  },
  getters: {
    getIsInitialPwd: state => state.isInitialPwd,
    getThemeList: state => state.themeList,
    getTheme: state => state.currentTheme, // 获取当前主题
    getInfo: state => state.info, // 获取登录信息
    getUser: state => state.info.user, // 获取用户信息
    getOrg: state => state.info.org, // 获取机构信息
    getRoles: state => state.info.roles, // 获取角色列表
    getAuths: state => state.info.auths || [], // 获取权限列表
    getApis: state => state.info.apis, // 获取接口列表
    columnsConfigData: state => state.columnsConfigData, // 获取可显示列信息
    avatarData: state => state.avatarData, // 用户头像信息
    getSysNoticeCount: state => state.sysNoticeCount, // 系统通知个数
    isAuth: state => auth => {
      // 是否拥有权限
      if (auth && state.info && state.info.auths && state.info.auths.length) {
        return state.info.auths.includes(auth)
      }
      return false
    },
    getAuthData: state => module => {
      console.log('module', module)
      // 根据模块获取权限数据
      if (!module || !state.info || !state.info.auths || !state.info.auths.length) {
        return {}
      }
      const btnAuthData = {}
      // 动态添加按钮权限
      state.info.auths
        .filter(i => i.includes(module))
        .forEach(i => {
          btnAuthData[i.substr(i.lastIndexOf(':') + 1, i.length - 1)] = true
        })
      return btnAuthData
    },
    getUserId: state => {
      if (state.info && state.info.user) {
        return state.info.user.userId
      }
      return i18n.t('NOTLOGIN')
    }
  },
  actions: {
    loadInfo: ({ dispatch, commit }, goIndex) => {
      // 加载登录信息
      return SystemApi.info()
        .then(info => {
          commit('setInfo', info)
          if (info) {
            dispatch('loadMenu', {
              sysId: config.sysId,
              roleIds: info.roles
            })
            dispatch('getRegions', { orgId: info.org.orgId }) // 加载区域站点机房
            // 获取用户配置类型
            dispatch('getCustomDict')
            // 加载可显示列信息
            dispatch('getViewConfig')
            // 加载用户头像
            dispatch('getAvatar')
            // 加载系统消息未读个数
            dispatch('getSysNoticeCount')
            // 告警配置
            dispatch('getConfigData', true)
            // 校验是否是初始密码
            commit('setIsInitialPwd', info.user ? info.user.password : '')
            if (goIndex) {
              router.push('/index')
            }
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 获取可显示列信息
    getViewConfig({ commit, state }) {
      userInfoApi
        .getViewConfig({ userId: state.info.user.userId })
        .then(res => {
          commit('setColumnsConfigData', res)
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 加载用户头像
    getAvatar({ commit, state }) {
      userInfoApi
        .getUserInfoByUserId({ userId: state.info.user.userId })
        .then(res => {
          commit('setUserAvatar', res)
        })
        .catch(err => {
          console.log(err)
        })
    },
    get_regionIdsByOrgId: ({ dispatch, commit, state }, request) => {
      return RegionApi.regionIdsByOrgId(request)
        .then(rsp => {
          commit('orgRegionIds', rsp)
          return rsp
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 获取系统未读个数
    getSysNoticeCount({ commit, state }) {
      SysNoticeApi.getSysNoticeCount()
        .then(res => {
          res = res || 0
          commit('setSysNoticeCount', res)
        })
        .catch(() => {})
    }
  },
  mutations: {
    setTheme: (state, theme) => {
      state.currentTheme = theme
    }, // 设置主题
    setInfo: (state, info) => (state.info = info), // 设置登录信息
    setIsInitialPwd: (state, pwd) => {
      // 123456经过md5加密之后是e10adc3949ba59abbe56e057f20f883e
      state.isInitialPwd = pwd.toLowerCase() === 'e10adc3949ba59abbe56e057f20f883e'
    },
    clearInfo: state => (state.info = {}), // 清除登录信息
    setColumnsConfigData: (state, data) => (state.columnsConfigData = data),
    setUserAvatar: (state, data) => (state.avatarData = data),
    setSysNoticeCount: (state, data) => (state.sysNoticeCount = data),
    addSysNoticeCount: (state, data) => state.sysNoticeCount++
  }
}
